package com.woniu.aimd.controller;

import cn.dev33.satoken.util.SaResult;

import com.woniu.aimd.pojo.DTO.NodeQueryDTO;
import com.woniu.aimd.pojo.DTO.StageInfo;
import com.woniu.aimd.pojo.entity.Node;
import com.woniu.aimd.service.NodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zcnovice
 * @data 2025/6/10 上午1:13
 */
@RequestMapping("/node")
@RestController
public class NodeController {
    
    @Autowired
    private NodeService nodeService;
    
    /**
     * @Description: 
     * @return: 
     * @Author:  zcnovice
     * @date:  2025/6/10 上午1:17
     */
    @PostMapping("/insert")
    public SaResult insert(@RequestBody Node node){
        try {
            Node result = nodeService.insert(node);
            return result!=null ? SaResult.ok("添加成功") : SaResult.error("添加失败");
        } catch (Exception e) {
            return SaResult.error("服务器异常: " + e.getMessage());
        }
    }

    /**
     * 根据userId、classId、stage[]获取节点（笔记）信息（nodeId、title、content）
     * @param nodeQueryInfo
     * @return
     */
    @PostMapping("getNodeInfo")
    public SaResult getNodeInfo(@RequestBody NodeQueryDTO nodeQueryInfo){
        List<StageInfo> stageInfos =  nodeService.getNodeInfo(nodeQueryInfo);
        return SaResult.ok().setData(stageInfos);
    }

    /**
     * 节点信息保存方法
     * @param node
     * @return
     * @Author:  ruirui
     * @date:  2025/6/11 下午19：26
     */
    @PutMapping("updateNode")
    public SaResult updateNodeInfo(@RequestBody Node node){
        System.out.println("修改node:"+node);
        boolean result = nodeService.updateNodeInfo(node);
        return result ? SaResult.ok("保存成功") : SaResult.error("更新失败");
    }

    /**
     * 根据NodeId获取当前节点信息
     * @param nodeId
     * @return
     * @Author:  ruirui
     * @date:  2025/6/11 下午19：26
     **/
    @GetMapping("getNodeById/{nodeId}")
    public SaResult getNodeById(@PathVariable String nodeId){
        System.out.println(nodeId);
        Node node = nodeService.getCurrentNode(nodeId);
        return SaResult.ok().setData(node);
    }
    /**
     * 文件删除
     */
    @DeleteMapping("deleteNode")
    public SaResult deleteNode( Node node){
        System.out.println(node);
        boolean result = nodeService.delete(node.getNodeId(),  node.getStage(), node.getClassId());
        return result ? SaResult.ok("删除成功") : SaResult.error("删除失败");
    }
}
